﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Text;
using System.IO;
public partial class admin_Admin_ProList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.GetProPages();
        }

    }
    private void GetProPages()
    {

        Products Pro = new Products();
        AspNetPager1.RecordCount = Pro.GetProCount();
        DataSet dr = Pro.GetProPage(AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "newspage");
        DataTable da = dr.Tables["newspage"];
        ProList.DataSource = da;
        ProList.DataKeyNames = new string[] { "Pid" };//主键
        ProList.DataBind();

    }

    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        GetProPages();
    }
    protected void ProList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //限制标题换行
        e.Row.Cells[1].Attributes.Add("style", "word-break :break-all ; word-wrap:break-word");
        //鼠标换背景色
        int i;
        //执行循环，保证每条数据都可以更新
        for (i = 0; i < ProList.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E6F5FA'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
        //删除提醒框
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[7].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除：\"" + e.Row.Cells[2].Text + "\"吗?')");
            }
        }

    }


    //删除
    protected void ProList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Products Pro = new Products();
        Pro.DelProduct(Convert.ToInt32(ProList.DataKeys[e.RowIndex].Value.ToString()));
        GetProPages();
    }




    protected void Button1_Click(object sender, EventArgs e)
    {

        Products Pro = new Products();
        for (int i = 0; i < ProList.Rows.Count; i++)
        {
            CheckBox cbox = (CheckBox)ProList.Rows[i].Cells[0].FindControl("del");
            if (cbox.Checked == true)
            {
                Pro.DelProduct(Convert.ToInt32(ProList.DataKeys[i].Value));
            }
        }
        GetProPages();
        Button2.Text = "全选";

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        CheckBox cb;
        if (Button2.Text == "全选")
        {
            for (int i = 0; i < ProList.Rows.Count; i++)
            {
                cb = (CheckBox)ProList.Rows[i].Cells[0].FindControl("del");
                cb.Checked = true;
            }
            Button2.Text = "取消";
        }
        else
        {
            for (int i = 0; i < ProList.Rows.Count; i++)
            {
                cb = (CheckBox)ProList.Rows[i].Cells[0].FindControl("del");
                cb.Checked = false;
            }
            Button2.Text = "全选";
        }

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string Toplianjie = WebSet.GetSingleValue("顶部连接");
        string hongxingwangzhan = WebSet.GetSingleValue("红星网站导航");
        string ProList = WebSet.GetProList2();
        string DibuLianjie = WebSet.GetSingleValue("底部版权");
        string WebTitle = WebSet.GetSingleValue("WebTitle").ToString();
        Products Pro = new Products();
        OleDbDataReader dr = Pro.GetProAll();
        while (dr.Read())
        {
            int Ptypeid = int.Parse(dr["Ptypeid"].ToString());
            string Pname = dr["Pname"].ToString();
            string Ptitle = dr["Ptitle"].ToString();
            string Pkey = dr["Pkey"].ToString();
            string Pjj = dr["Pjj"].ToString();
            string maodian = dr["maodian"].ToString();
            string HtmlUrl = dr["HtmlUrl"].ToString();
            string Pconn = dr["Pconn"].ToString();
            string Psimg = dr["Psimg"].ToString();
            string Pid = dr["Pid"].ToString();
            string dianhua = WebSet.GetProPTypeForPTypeId(Ptypeid, "Pphone");
            string ProType = "<a href=\"ProductList.aspx?PType=" + Ptypeid + "\">" + WebSet.GetProPTypeForPTypeId(Ptypeid, "Pname") + "</a>";
            Ptitle += WebTitle;
            //生成页面
            try
            {
                StringBuilder strhtml = new StringBuilder();
                //创建StreamReader对象
                using (StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/") + "ProMoBan.html", Encoding.GetEncoding("gb2312")))
                {
                    String oneline;
                    //读取指定的HTML文件模板
                    while ((oneline = sr.ReadLine()) != null)
                    {
                        strhtml.Append(oneline);
                    }
                    sr.Close();
                }
                //StringBuilder strhtml = html;
                string FilePath = "";
                strhtml = strhtml.Replace("$title", Ptitle);
                strhtml = strhtml.Replace("$keywords", Pkey);
                strhtml = strhtml.Replace("$description", Pjj);
                strhtml = strhtml.Replace("$ProductName", Pname);
                strhtml = strhtml.Replace("$conn", Pconn);
                strhtml = strhtml.Replace("$ProImg", Psimg);
                strhtml = strhtml.Replace("$danqiandizhi", HtmlUrl);
                strhtml = strhtml.Replace("$jianjie", Pjj);
                strhtml = strhtml.Replace("$Toplianjie", Toplianjie);
                strhtml = strhtml.Replace("$hongxingwangzhan", hongxingwangzhan);
                strhtml = strhtml.Replace("$ProList", ProList);
                strhtml = strhtml.Replace("$dianhua", dianhua);
                strhtml = strhtml.Replace("$DibuLianjie", DibuLianjie);
                strhtml = strhtml.Replace("$ProType", ProType);
                strhtml = strhtml.Replace("$maodian", maodian);


                FilePath = System.Web.HttpContext.Current.Server.MapPath("~/") + HtmlUrl;
                //删除原有文件
                FileInfo file = new FileInfo(FilePath);
                if (file.Exists)
                {
                    file.Delete();
                }
                //创建文件信息对象--------------------------------------------
                FileInfo finfo = new FileInfo(FilePath);
                //以打开或者写入的形式创建文件流
                using (FileStream fs = finfo.OpenWrite())
                {
                    //根据上面创建的文件流创建写数据流
                    StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
                    //把新的内容写到创建的HTML页面中
                    sw.WriteLine(strhtml);
                    sw.Flush();
                    sw.Close();
                }
            }
            catch (Exception err)
            {
                //输出异常信息
                throw new Exception(err.Message, err);
            }
        }
        dr.Close();
        dr.Dispose();
        Response.Write("<script>alert('生成完成！')</script>");

    }
}
